package com.gopay.checkorder.service.impl;

import java.util.List;
import javax.annotation.Resource;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.checkorder.service.BankOrderService;
import com.gopay.checkorder.service.task.BankCountDownLatch;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.GdbConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.gdb.ebank.GdbB2cMessage;
import com.gopay.common.domain.bank.dayend.BaseDayendEntry;
import com.gopay.common.domain.cashier.BankCardType;
import com.gopay.common.domain.cps.vo.CheckOrderResult;
import com.gopay.common.domain.cps.vo.InAccInfo;
import com.gopay.remote.bank.BankInteractRemoteService;

/**
 * 广发银行B2C网关订单查询
 * 
 * @CheckOrderGdbB2cImpl.java
 * @author zhengxue
 * @2014-11-13 下午4:41:19 www.gopay.com.cn Inc.All rights reserved.
 */
public class CheckOrderGdbB2cImpl implements BankOrderService {

    private final static Logger logger = LoggerFactory.getLogger("GDB");

    @Resource(name = "bankClient")
    private BankInteractRemoteService bankClient;

    @Override
    public CheckOrderResult checkOrder(BankOrder bankOrder, String target) {
        CheckOrderResult checkOrderResult = new CheckOrderResult();
        checkOrderResult.setSuccess(false);
        checkOrderResult.setOrderId(bankOrder.getOrderId());

        GdbB2cMessage base = new GdbB2cMessage();
        base.setOrderId(bankOrder.getOrderId());
        base.setBankOrderId(bankOrder.getOutOrderId());
        base.setBank(BankConstants.BANK_LOGO_GDB);
        base.setBankIntrChann(BisBankInterfaceChannel.get(bankOrder.getInterfaceChennelCode()));
        base.setBankBusProd(BisBankBusinessProd.get(bankOrder.getBankBizProdCode()));

        Result result = new Result();
        try {
            result = bankClient.query(base);
            if (result == null || !result.isSuccess()) {
                logger.error("GDB bankClient fail.");
                if (result == null || ProcCodeConstants.PROC_CODE_100E6037.equals(result.getResultCode())) {
                    BankCountDownLatch.failIncrease(bankOrder.getBankCode());
                    checkOrderResult.setCommunicateError(true);
                }
                logger.error(ToStringBuilder.reflectionToString(result, ToStringStyle.DEFAULT_STYLE));
                checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
                checkOrderResult.setErrMsg("系统返回对账结果为空");
                return checkOrderResult;
            }

            GdbB2cMessage res = (GdbB2cMessage) result.getDefaultModel();
            if (res == null) {
                // 查询订单为空
                logger.error("GDB fail:orderId: " + bankOrder.getOrderId() + " errcode: "
                        + ProcCodeConstants.PROC_CODE_100E5062 + " err:bis query is null.");
                checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E5062);
                checkOrderResult.setErrMsg("银行返回对账结果为空");
            }

            if (!(GdbConstants.QUERY_STATUS_SUCCESS.equals(res.getOrderStatus()))) {
                // 查询订单失败(1 成功 2 失败 3异常)
                logger.error("GDB fail:orderId: " + bankOrder.getOrderId() + "orderStatus: " + res.getOrderStatus()
                        + "retCode: " + res.getRetCode());
                checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E5062);
                checkOrderResult.setErrMsg("orderStatus= " + res.getOrderStatus() + " retCode= " + res.getRetCode());
                return checkOrderResult;
            } else {
                // 查询成功
                // 成功处理
                if (GdbConstants.PAY_STATUS_SUCCESS.equalsIgnoreCase(res.getRetCode())) {
                    logger.error("广发B2C订单 " + bankOrder.getOrderId() + "交易状态为 " + GdbConstants.PAY_STATUS_SUCCESS
                            + "，成功");
                    checkOrderResult.setSuccess(true);
                    InAccInfo accInfo = new InAccInfo();
                    accInfo.setOrderId(bankOrder.getOrderId());
                    accInfo.setAmount(res.getAmount());
                    accInfo.setRetCode("00");
                    accInfo.setCardType(BankCardType.Unknown.toString());
                    checkOrderResult.setAccInfo(accInfo);
                }
                // 失败处理
                else if (!(GdbConstants.PAY_STATUS_SUCCESS.equalsIgnoreCase(res.getRetCode()))) {
                    checkOrderResult.setBankFailure(false);
                    logger.error("广发B2C订单 " + bankOrder.getOrderId() + "交易状态为 " + res.getRetCode() + "，失败");
                    checkOrderResult.setErrMsg("订单处理失败");
                }
            }
            return checkOrderResult;

        } catch (Exception e) {
            logger.error("GDB bankClient fail.", e);
            checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E6022);
            checkOrderResult.setErrMsg("对账调用bis失败");
            return checkOrderResult;
        }
    }
    
    @Override
	public BankOrder getDbOrder() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public BaseMessage dayend(String bank_bus) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<CheckOrderResult> checkDayend(String bank_bus) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Result getDayendMsg(String string) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public CheckOrderResult checkDayendOrder(BaseDayendEntry bdm, BankOrder bo) {
		// TODO Auto-generated method stub
		return null;
	}
}
